/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.eclass.servlet.front;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;

import cn.eclass.bean.api.IGiftBean;
import cn.eclass.entity.GiftCategoryEntity;
import cn.eclass.entity.GiftEntity;
import cn.eclass.entity.GiftOrderEntity;
import cn.eclass.entity.UserEntity;
import cn.eclass.servlet.ActionFace;
import cn.eclass.utils.BeanFactory;
import cn.eclass.utils.ConstantsConf;
import cn.eclass.utils.Helper;

import com.google.gson.Gson;

/**
 *
 * @author fortruth
 */
public class Shop extends ActionFace {

	
    IGiftBean bean = beanFactory.getGiftBean();
    
    private Map<String, List<GiftEntity>> buildMap(List<GiftCategoryEntity> giftCatList) {
    	HashMap<String, List<GiftEntity>> map = new HashMap<String, List<GiftEntity>>();
    	for(GiftCategoryEntity cat : giftCatList) {
    		List<GiftEntity> list = bean.getGiftByCat(cat.getId());
    		if(list != null && list.size() != 0) map.put(cat.getName(), list);
    	}
    	return map;
    }
    
    public void doOrder() {
//        String userName = (String) session.getAttribute(ConstantsConf.SESSION_USERNAME);
        int userId = user.getId();
        int giftId = Integer.parseInt(request.getParameter("giftId"));
        int createOrderSuccess = 0;
        int createdeductPointsSuccess = 0;
        GiftOrderEntity gOrderEnt = null;
        int gOrderId = 0;
        try{
	        gOrderEnt = new GiftOrderEntity();
	        gOrderEnt.setGiftId(giftId);
	        gOrderEnt.setUserId(userId);
	        gOrderEnt = beanFactory.getGiftOrder().add(gOrderEnt);
	        gOrderId = gOrderEnt.getId();
	        createOrderSuccess = 1;
	        GiftEntity gEnt = beanFactory.getGiftBean().get(giftId);
	        user.setPoints(user.getPoints() - gEnt.getPoints());
	        beanFactory.getUserBean().update(user);
	        createdeductPointsSuccess = 1;
        } catch(Exception e) {
        	e.printStackTrace();
        }
        Map result = new HashMap();
        if(createdeductPointsSuccess == 1) {
        	session.removeAttribute(ConstantsConf.GIFTORDERLIST);
            result.put("status", "success");
        } else{
        	if(createOrderSuccess == 1) beanFactory.getGiftOrder().delete(gOrderId);
        	result.put("status", "failed");
        }
        
        out.print(new Gson().toJson(result));
    	
    }
    
    @Override
    public void doIndex() {
        try {
            List<GiftEntity> gifts = bean.list();
            request.setAttribute("gifts", gifts);
            request.setAttribute("columnsPerPage", ConstantsConf.columnsPerPage);
            request.setAttribute("rowsPerPage", ConstantsConf.rowsPerPage);

            List<GiftCategoryEntity> giftCatList = BeanFactory.factory.getGiftCategoryBean().list();
            request.setAttribute("giftCatList", giftCatList);
            
            request.setAttribute("giftMap", buildMap(giftCatList));
            //
            request.getRequestDispatcher(view).forward(request, response);
            
        } catch (ServletException ex) {
            Logger.getLogger(Shop.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Shop.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
